package com.seaengine.actor
{
	import com.seaengine.actor.anibmp.AnimationBMPMgr;
	import com.seaengine.actor.anibmp.BaseBmpNodeInfo;
	import com.seaengine.actor.anibmp.characterbmp.CharacterBmpMgr;
	import com.seaengine.actor.node.BaseNode;
	import com.seaengine.actor.node.CharacterNode;
	import com.seaengine.actor.node.NodeWrapper;
	import com.seaengine.core.timer.AnimationTimer;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.setTimeout;
	
	public class NPCActor extends Actor
	{	
		public static const ACTION_INVALID_ID:int = -1;
		public static const ACTION_IDLE:int = 0;
		public static const ACTION_WALK:int = 1;
		public static const ACTION_RUN:int = 2; 
		public static const ACTION_DEATH:int = 3;
		public static const ACTION_DEAD:int = 10;
		public static const ACTION_RUN_PRE:int = 40;
		public static const ACTION_RUN_STOP:int = 41;
		public static const BEGIN_JUMP:int = 4;
		public static const ACTION_JUMP_PRE:int = 4;
		public static const ACTION_JUMP_UP:int = 5;
		public static const ACTION_JUMP_DOWN:int = 6;
		public static const ACTION_JUMP_FINISH:int = 7;
		public static const ACTION_JUMP_FINISHED:int = 8;	
		public static const END_JUMP:int = 7;
		public static const MAX_TOLERANCE:int = 32;//
		
		public static const ACTION_FIGHT_IDLE:int = 20;		
		public static const ACTION_FIGHT_ATTACKED:int = 21;//被击中
		public static const ACTION_FIGHT_PARRY:int = 22;//格挡
		public static const ACTION_FIGHT_EVADE:int = 23;//闪避
		public static const ACTION_FIGHT_MISS:int = 24;//主攻miss
		
		public static const ACTION_FIGHT_APPROACH:int = 24;/**当对方QTE时，己方蓄力动作*/
		
		public static const ACTION_FIGHT_ATTACKER_QTE:int = 31;
		public static const ACTION_FIGHT_ATTACKEE_QTE:int = 32;
		public static const ACTION_FIGHT_CHAOSHA:int = 35;
		
		public static const ACTION_CURING:int = 51;/**回气动作*/
		
		public static const ACTION_COMMON_ATTACK_BASE:int = 2001;
		public static const ACTION_COMMON_ATTACK_TAUNT:int = 201;
		public static const ACTION_COMMON_ATTACK_VICTORY:int = 202;
		
		
		public var WALK_SPEED:int = 16;
		public var JUMP_REPOWER:int=15;
		public var JUMP_SPEED:int = 8;
		public var MAX_JUMP_SPEED:int = 45;
		
		public var MAX_HEIGHT:int = 176;
		public var MAX_DISTANCE:int = 360;
		
		protected var _graphicNode:NodeWrapper
		/**
		 * 正在做的动作 
		 */
		protected var m_currentAction:int;
		protected var _type:uint;
		
		//目标点 (如果没有actor，则为point点为目标 )
		public var targetPoint:Point;
		public var targetActor:Actor;
		public var m_ScaleValue:Number = 1;
		//当前速度
		private var m_xpower:Number;
		private var m_xSpeed:Number;//x
		private var m_yVelocity:Number;//y
		//当前方向（-1为左 ,1为右）
		private var m_currentDir:int;	
		//跳跃		
		public static const GRAVITY:Number = 2.4;//gravity /ˈgrævɪti/ 重力 
		public static const START_POWER:int = -40;
		
		public function NPCActor( _id:uint,_name:String,_typeVaule:uint)
		{			
			super(_id);
			_type = _typeVaule;
			actorName = _name;
			init();
		}
		public function set Dirction(_val:int):void
		{
			m_currentDir = _val;
			
		}
		
		public function get Dirction():int
		{
			return m_currentDir;
		}
		
		public function get type():uint
		{
			return _type;
		}

		public function set type(value:uint):void
		{
			_type = value;
		}

		protected function init():void
		{
			_graphicNode = new NodeWrapper(this, new CharacterNode(this),_type);
			addChild(_graphicNode);
		}
		
		/**
		 * 根据动画id做动作
		 * @param animationID 动作id
		 * 
		 */
		public function doActionByAniID(_animationID:int):void
		{
			AnimationBMPMgr.getInstance().getAnimationBmpInfo(_type,_animationID,aniBmpInfoGetted);
		}
		
		protected function aniBmpInfoGetted(_nodeinfo:BaseBmpNodeInfo):void
		{
			trace ("获取动画数据成功，开始播放动画");
			_graphicNode.doActionByNode(_nodeinfo);
		}
		
		/**
		 * 移动，做动作 
		 * @param e
		 * 
		 */
		protected function onPerformMovement(e:Event):void
		{
			
		}
		
		/**
		 * 更新移动属性 比如x,y方向上的速度 
		 * 
		 */
		public function updateMoveproperty():void
		{	
			if(targetPoint)
			{
				if(m_currentAction!= ACTION_JUMP_FINISH)
				{
					if((Math.abs(targetPoint.x - x ) > MAX_TOLERANCE)&&m_xpower==0)
					{
						Dirction = targetPoint.x >= x ? 1: -1;
					}
				}
				if(isJump())
				{
					//m_xSpeed = Math.abs(targetPoint.x - x) <= PlayerController.BOARDER_WIDTH/2 ? 0:JUMP_SPEED*m_ScaleValue-m_xpower;
					m_xSpeed = Math.abs(targetPoint.x - x) <= 2 ? 0:JUMP_SPEED*m_ScaleValue-m_xpower;
					if(m_xpower!=0)
					{
						m_xpower+=JUMP_REPOWER;
					}
					if(m_xSpeed<0)
					{
						m_xpower=0;
					}
				}
				else
				{
					//m_xSpeed = Math.abs(targetPoint.x - x) <= PlayerController.BOARDER_WIDTH/2 ? 0:WALK_SPEED;
					m_xSpeed = Math.abs(targetPoint.x - x) <= 2 ? 0:WALK_SPEED;
				}
				
			}
			else if(targetActor)
			{
				if(m_currentAction != ACTION_JUMP_FINISH && m_xpower==0)
				{
					if(Math.abs(targetActor.x - x ) > MAX_TOLERANCE)
					{
						Dirction = targetActor.x >= x ? 1: -1;			
					}
				}
				if(isJump())
				{
					m_xSpeed = Math.abs(targetActor.x - x) <= MAX_TOLERANCE ? 0:JUMP_SPEED-m_xpower;
					if(m_xpower!=0)
					{
						m_xpower+=JUMP_REPOWER;
					}
					if(m_xSpeed<0)
					{
						m_xpower=0
					}	
				}
				else
				{
					m_xSpeed = Math.abs(targetActor.x - x) <= MAX_TOLERANCE ? 0:WALK_SPEED;	
				}							
				
			}
			else
			{
				m_xSpeed = 0;
				m_xpower = 0;
			}
			
			m_yVelocity = 0;
			
			
			if( m_currentAction == ACTION_JUMP_PRE)
			{
				m_xSpeed = 0;
				m_xpower = 0;
				return;
			}
			
			if(m_currentAction == ACTION_JUMP_UP)
			{
				if(targetPoint)
				{
					if(Math.abs(targetPoint.x - x ) <= 2)
					{
						m_xSpeed = 0;	
					}
				}
				if(targetActor)
				{
					if(Math.abs(targetActor.x - x ) <= MAX_TOLERANCE)
					{
						m_xSpeed = 0;	
					}
				}
				
				m_yVelocity = START_POWER * m_ScaleValue;
				
			}
			else if( m_currentAction == ACTION_JUMP_DOWN )
			{				
				m_yVelocity = 0;
				
			}
			if( m_currentAction == ACTION_JUMP_FINISH)
			{	
				m_xSpeed = 0;				
			}
			m_xSpeed *= m_ScaleValue;
		}
		
		public function isJump():Boolean
		{
			if(m_currentAction >= BEGIN_JUMP && m_currentAction <= END_JUMP)
			{
				return true;
			}
			return false;
		}	
	}	
}